package cn.edu.nuc.zyb.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.zyb.client.UserClient;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserRegist extends JFrame {

	private JPanel contentPane;
	private JTextField textField1;
	private JPasswordField textField2;
	private JTextField textField3;
	private JTextField textField4;

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserRegist frame = new UserRegist();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public UserRegist() {
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 320, 401);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("用户名：");
		label.setBounds(49, 41, 72, 18);
		contentPane.add(label);

		textField1 = new JTextField();
		textField1.setBounds(119, 38, 86, 24);
		contentPane.add(textField1);
		textField1.setColumns(10);

		JLabel label_1 = new JLabel("密码：");
		label_1.setBounds(49, 88, 72, 18);
		contentPane.add(label_1);

		textField2 = new JPasswordField();
		textField2.setBounds(119, 85, 86, 24);
		contentPane.add(textField2);

		JLabel label_2 = new JLabel("姓名：");
		label_2.setBounds(49, 137, 72, 18);
		contentPane.add(label_2);

		textField3 = new JTextField();
		textField3.setBounds(119, 134, 86, 24);
		contentPane.add(textField3);
		textField3.setColumns(10);

		JLabel label_3 = new JLabel("年龄：");
		label_3.setBounds(49, 181, 72, 18);
		contentPane.add(label_3);

		textField4 = new JTextField();
		textField4.setBounds(119, 178, 86, 24);
		contentPane.add(textField4);
		textField4.setColumns(10);

		JButton button = new JButton("提交");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String username = textField1.getText();
				int password = Integer.valueOf(textField2.getText());
				String name = textField3.getText();
				int age = Integer.valueOf(textField4.getText());
				
				UserClient uc = new UserClient();
				uc.addUser(username, password, name, age);
				if (uc.acceptMsg()) {
					JOptionPane.showMessageDialog(null, "注册成功");
					UserRegist.this.setVisible(false);
				} else {
					JOptionPane.showMessageDialog(null, "注册失败");
				}
			}
		});
		button.setBounds(131, 244, 72, 27);
		contentPane.add(button);
	}
}
